#ifndef __flash_h
#define __flash_h

#define f_qspi_en				1
#define f_spi				QSPI1
#define	f_spi_data			QSPI1_DATA
#define f_spi_AHB			RCC_AHBPeriphClkEn_QSPI1

#define f_spi_cs_AHB		RCC_AHBPeriphClkEn_GPIOB
#define f_spi_cs_gpio		GPIOB
#define f_spi_cs_pin		GPIO_Pin_5
#define f_spi_cs_pinscr		GPIO_PinSource5
#define f_spi_cs_AF			PB5_QSPI1_NSS

#define f_spi_scl_AHB		RCC_AHBPeriphClkEn_GPIOF
#define f_spi_scl_gpio		GPIOF
#define f_spi_scl_pin		GPIO_Pin_0
#define f_spi_scl_pinscr	GPIO_PinSource0
#define f_spi_scl_AF		PF0_QSPI1_SCK


//B7	A10		F2		
#define f_spi_mosi_AHB		RCC_AHBPeriphClkEn_GPIOA    
#define f_spi_mosi_gpio		GPIOA
#define f_spi_mosi_pin		GPIO_Pin_10
#define f_spi_mosi_pinscr	GPIO_PinSource10
#define f_spi_mosi_AF		PA10_QSPI1_MOSI

//	A9	B6		F1	F3	F4
#define f_spi_miso_AHB		RCC_AHBPeriphClkEn_GPIOF
#define f_spi_miso_gpio		GPIOF
#define f_spi_miso_pin		GPIO_Pin_1
#define f_spi_miso_pinscr	GPIO_PinSource1
#define f_spi_miso_AF		PF1_QSPI1_MISO

//A 	5	11	13	B4	F3
#define f_spi_o2_AHB		RCC_AHBPeriphClkEn_GPIOA
#define f_spi_o2_gpio		GPIOA
#define f_spi_o2_pin		GPIO_Pin_5
#define f_spi_o2_pinscr		GPIO_PinSource5
#define f_spi_o2_AF			PA5_QSPI1_DIO2
#define f_spi_o2_normal		((uint8_t)0x00)
#define f_spi_o2_wrbit(x)	GPIO_WriteBit(f_spi_o2_gpio,f_spi_o2_pin,x)

//A			6	12	14	B5
#define f_spi_o3_AHB		RCC_AHBPeriphClkEn_GPIOA
#define f_spi_o3_gpio		GPIOA
#define f_spi_o3_pin		GPIO_Pin_6
#define f_spi_o3_pinscr		GPIO_PinSource6
#define f_spi_o3_AF			PA6_QSPI1_DIO3
#define f_spi_o3_normal		((uint8_t)0x00)
#define f_spi_o3_wrbit(x)	GPIO_WriteBit(f_spi_o3_gpio,f_spi_o3_pin,x)

//spi normal
//#define f_cs_enable()		SPI_Read_Cmd(f_spi, ENABLE);  \
//							SPI_Lock_CS_Cmd(f_spi, ENABLE); \
//							SPI_RunCmd(f_spi, ENABLE)
#if f_qspi_en	
#define f_cs_enable(x)		SPI_Read_Cmd(f_spi, DISABLE);  \
							SPI_SendCommand(f_spi,x);\
							SPI_Lock_CS_Cmd(f_spi, ENABLE); \
							SPI_RunCmd(f_spi, ENABLE);\
							while (SPI_GetBusyStatus(f_spi) == SET){}
#else
#define f_cs_enable(x)		SPI_SendCommand(f_spi,x);\
							SPI_Read_Cmd(f_spi, DISABLE);  \
							SPI_Lock_CS_Cmd(f_spi, ENABLE); \
							SPI_RunCmd(f_spi, ENABLE);\
							while (SPI_GetBusyStatus(f_spi) == SET){}
#endif
							
#define f_cs_disable()		SPI_RunCmd(f_spi, DISABLE); \
							SPI_Lock_CS_Cmd(f_spi, DISABLE)
								
								
#define f_spi_sendbyte(x)	SPI_SendData(f_spi,(x)&0xff);\
							while (SPI_GetBusyStatus(f_spi) == SET){}
								
								
#define flash_page_wr(x,y,z)			flash_sectorErase(x);flash_page_wr_((x),(y),(z))

//spi q


#define flash_dummy_byte		0x00
#define flash_write_enable		0x06
#define flash_rd_status_reg_1	0x05
#define flash_rd_status_reg_2	0x35
#define flash_rd_status_reg_3	0x15
#define flash_wr_status_reg_2	0x31
#define flash_wr_status_reg_3	0x11
#define flash_page_program		0x02
#define flash_sector_erase		0x20
#define flash_block_erase_32kb	0x52
#define flash_block_erase_64kb	0xd8
#define flash_chip_erase		0x60
#define flash_rd_data			0x03

#define flash_read_para			0xc0
#define flash_qspi_page_program	0x32
#define flash_qspi_rd_date		0x0b
#define flash_qspi_rd_date_6	0x6b
#define	flash_enter_qspi_mode	0x38
#define	flash_exit_qspi_mode	0xff
	
	
void flash_init(void);
	
void flash_writeEn(void);
void flash_waitbusy(void);//(uint8_t* mid,uint16_t* did )
void flash_sectorErase(uint32_t addr);
void flash_page_wr_(uint32_t addr,const uint8_t data[],uint16_t cnt);
void flash_read_data(uint32_t addr, uint8_t data[],uint32_t cnt);
u8 f_spi_readbyte(void)	;

void flash_wr_image(uint16_t x,const uint8_t* data,uint16_t cnt);
void flash_read_data_q(uint32_t addr, uint8_t data[],uint32_t cnt);


#endif
